package com.ruoyi.client.controller;

import cn.dev33.satoken.secure.BCrypt;
import com.ruoyi.client.domain.entity.BlogUser;
import com.ruoyi.client.service.IBlogUserService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("client/user")
public class BlogUserController extends BaseController {
    @Autowired
    IBlogUserService userService;
    @GetMapping("list")
    public TableDataInfo<BlogUser> list(PageQuery pageQuery){
        TableDataInfo<BlogUser> result = userService.selectBlogUserList(pageQuery);
        return result;
    }
    @PostMapping
    public R add(@RequestBody BlogUser user){
        user.setPassword(BCrypt.hashpw(user.getPassword()));
        boolean save = userService.save(user);
        return toAjax(save);
    }
    @GetMapping("/{id}")
    public R<BlogUser> getInfor(@PathVariable Long id){
        BlogUser user = userService.getById(id);
        return R.ok(user);
    }
    @PutMapping
    public R update(@RequestBody BlogUser user){
        boolean b = userService.updateById(user);
        return toAjax(b);
    }
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long[] id){
        boolean b = userService.removeBatchByIds(Arrays.asList(id));
        return toAjax(b);
    }
    @PutMapping("resetPwd")
    public R resetPdw(@RequestBody BlogUser user){
        user.setPassword(BCrypt.hashpw(user.getPassword()));
        boolean b = userService.updateById(user);
        return toAjax(b);
    }
}
